package com.yequan.thread.keyword.volatileword;

/**
 * @author : Administrator
 * @date : 2020/5/23
 */
public class VolatileTest {

    public static volatile int race = 0;

    public static void increase() {
        race++;
    }

    public static final int COUNT = 20;

    public static void main(String[] args) {
        Thread[] threads = new Thread[COUNT];
        for (int i = 0; i < COUNT; i++) {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < 100; j++) {
                    increase();
                }
            });
            threads[i].start();
        }

        while (Thread.activeCount() > 1) {
            Thread.yield();
        }
        System.out.println(race);
    }

}
